<?php 
/*
 module:		用户
 create_time:	2024-04-25 14:08:19
 author:		
 contact:		
*/

namespace app\api\service\Person;
use app\api\model\Person\User;
use think\facade\Db;
use think\facade\Log;
use think\exception\ValidateException;
use xhadmin\CommonService;

class UserService extends CommonService {

    /**start**/
	/*
 	* @Description  列表数据
 	*/
	public static function userInfo($uid){
		try{
			$userInfo = User::where(["uid"=>$uid])->find()->toArray();
            $wxUser = db("user_wechat")
                ->where("uid",$uid)->find();
            $array = [];
            if(!empty($wxUser)){
                $array["wxUser"]["nickname"] = $wxUser["nickname"];
                $array["wxUser"]["headimgurl"] = $wxUser["headimgurl"];
            }else{
                $array["wxUser"]["nickname"] = "用户".substr($userInfo["phone"],7,4);
                $array["wxUser"]["headimgurl"]="";
            }

		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return $array;
	}

    public static function mbtiOrderList($uid,$limit,$page){
        try{
//			$userInfo = User::where(["uid"=>$uid])->find()->toArray();
            $list = Db::name("order")
                ->where("uid",$uid)
                ->where("outcome","NOT NULL")
                ->field("title,order_id,create_time,type,state,order_son,update_time")
                ->paginate([
                    "list_rows"=>$limit,
                    "query"=>[
                        "page"=>$page
                    ]
                ])->toArray();
            foreach ($list['data'] as $k=>$v){
                $list["data"][$k]["img"] = "https://mbtics.guoxuejianyi.com/images/4.png";
                $list["data"][$k]["title"] = 'Mbti题：'.$v['title'];
                $list["data"][$k]["count"] = Db::name("order_share")->where(["oid"=>$v['order_id']])->count();
                $list["data"][$k]["remain_count"] = (3 - $list["data"][$k]["count"]);
                //时间加上24小时
                $time = $v["update_time"] + 86400;
                //减去当前时间就超过了24小时就更新状态失效了
                $remain_time = $time - time();
                if($remain_time < 0){
                    //失效了 更新状态
                    Db::name("order")->where("order_id",$v["order_id"])->update(["state"=>-1]);
                    $list["data"][$k]["state"] = -1;
                }
                $list["data"][$k]["time"] =$remain_time;

                $imgs = Db::name("order_share")
                    ->alias("os")
                    ->join("user_wechat uw","os.uid = uw.uid","left")
                    ->where("os.oid",$v['order_id'])
                    ->field("uw.headimgurl")
                    ->select()
                    ->toArray();
                if(empty($imgs)){
                    $imgs = [];
                }
                $list["data"][$k]["imgs"] = $imgs;
            }
//            $array = ["wxUser"=>$wxUser];
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }
        return $list;
    }

    public static function friendList($uid,$limit,$page)
    {
        try{
//			$userInfo = User::where(["uid"=>$uid])->find()->toArray();
            $list = Db::name("user_subordinate")
               ->alias("us")
               ->join("user_wechat uw","us.uid = uw.uid")
               ->where(["us.pid"=>$uid])
               ->paginate([
                   "list_rows"=>$limit,
                   "query"=>[
                       "page"=>$page
                   ]
               ])->toArray();

//            $array = ["wxUser"=>$wxUser];
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }
        return $list;
    }

    /**end**/



}

